<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>模板</title>
   
</head>
  <!--引入开发版本Vue-->
  <script type="text/javascript" src="../js/vue.js"></script>
<body>

    <!-- 
        v-for指令
             1.用于展示列表数据
             2.语法: v-for="(item,index) in xxx" :key="yyy"
             3.可遍历: 数组,对象,字符串(用的很少),指定次数(用的很少)
     -->
    
    <!-- 准备好一个容器 -->
    <div id="root">
        <!-- 遍历数组 -->
        <h2>人员列表</h2>
        <input type="text" v-model="keyWord" placeholder="请输入名字">
       <ul>
          <li v-for="(p,index) of filPerons" :key="index">
           {{p.name}} - {{p.age}}-{{p.sex}}
         </li>
       </ul>
       
   
    </div>
</body>

<script type="text/javascript">
    Vue.config.productionTip = false //阻止 vue 在启动时生成生产提示

    //用watch实现
    /* new Vue({
        el:document.getElementById('root'),
        data:{
           keyWord:'',
            persons:[
              {id:'001', name:'马冬梅',age:18,sex:'女'},
              {id:'002', name:'周冬雨',age:28,sex:'女'},
              {id:'003', name:'周杰伦',age:38,sex:'男'},
              {id:'004', name:'温兆龙',age:38,sex:'男'}
           ],
           filPerons:[]
         },
         watch:{
          keyWord:{
            immediate:true,//初始化时让handle调用一下
            handler(val){
              this.filPerons = this.persons.filter((p)=>{
                return p.name.indexOf(val) !== -1
            })
          }
        }
      }
    }) */
    
    //用computed实现
 /*    new Vue({
        el:document.getElementById('root'),
        data:{
           keyWord:'',
            persons:[
              {id:'001', name:'马冬梅',age:18,sex:'女'},
              {id:'002', name:'周冬雨',age:28,sex:'女'},
              {id:'003', name:'周杰伦',age:38,sex:'男'},
              {id:'004', name:'温兆龙',age:38,sex:'男'}
           ],
         },
         computed:{
            filPerons(){
              return  this.filPerons = this.persons.filter((p)=>{
                  return p.name.indexOf(this.keyWord) !== -1
              })
            }//end
        }
    }) */

  

</script>

</html>